package com.lz.base.project.thread.runnable;

import cn.hutool.core.date.StopWatch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 */
public class RunableLz {

    public static void main(String[] args) {
        // t1();
        //  t2();
        t3();
    }

    public static void t1() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Thread thread1 = new Thread(new ThreadRunable("上海"));
        Thread thread2 = new Thread(new ThreadRunable("北京"));
        Thread thread3 = new Thread(new ThreadRunable("广州"));
        Thread thread4 = new Thread(new ThreadRunable("深圳"));
        Thread thread5 = new Thread(new ThreadRunable("杭州"));
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
        System.out.println("ThreadRunable结束");
        stopWatch.stop();
        System.out.println("ThreadRunable总耗时：" + stopWatch.getTotalTimeMillis());
    }

    public static void t2() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        CountDownLatch countDownLatch = new CountDownLatch(5);
        Thread thread1 = new Thread(new ThreadRunableLatch("上海", countDownLatch));
        Thread thread2 = new Thread(new ThreadRunableLatch("北京", countDownLatch));
        Thread thread3 = new Thread(new ThreadRunableLatch("广州", countDownLatch));
        Thread thread4 = new Thread(new ThreadRunableLatch("深圳", countDownLatch));
        Thread thread5 = new Thread(new ThreadRunableLatch("杭州", countDownLatch));
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("ThreadRunableLatch结束");
        stopWatch.stop();
        System.out.println("ThreadRunableLatch总耗时：" + stopWatch.getTotalTimeMillis());
    }

    public static void t3() {

        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ExecutorService service = Executors.newFixedThreadPool(5);

        ThreadRunable qfe = new ThreadRunable("上海");
        service.execute(qfe);
        qfe = new ThreadRunable("北京");
        service.execute(qfe);
        qfe = new ThreadRunable("杭州");
        service.execute(qfe);
        qfe = new ThreadRunable("广州");
        service.execute(qfe);
        qfe = new ThreadRunable("深圳");
        service.execute(qfe);
        System.out.println("t3-ThreadRunable结束");
        stopWatch.stop();
        System.out.println("t3-ThreadRunable总耗时：" + stopWatch.getTotalTimeMillis());
    }
}
